package com.zyao89.view.zloading.circle;

import com.zyao89.view.zloading.ZLoadingBuilder;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/10/2.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */
public class SingleCircleBuilder extends ZLoadingBuilder {
    private static final int OUTER_CIRCLE_ANGLE = 320;

    // 最终阶段
    private static final int FINAL_STATE = 2;

    // 当前动画阶段
    private int mCurrAnimatorState = 0;
    private Paint mStrokePaint;
    private RectFloat mOuterCircleRectF;

    // 旋转开始角度
    private int mStartRotateAngle;

    // 旋转角度
    private int mRotateAngle;
    private Arc mArc;

    @Override
    protected void initParams(Context context, Color color) {
        mArc = new Arc();

        // 最大尺寸
        float outR = getAllSize();

        // 小圆尺寸
        float inR = outR * 0.6f;

        // 初始化画笔
        initPaint(inR * 0.4f, color);

        // 旋转角度
        mStartRotateAngle = 0;

        // 圆范围
        mOuterCircleRectF = new RectFloat();
        mOuterCircleRectF.fuse(NumCalcUtil.subtract(getViewCenterX(), outR), NumCalcUtil.subtract(getViewCenterY(), outR), NumCalcUtil.add(getViewCenterX(), outR), NumCalcUtil.add(getViewCenterY(), outR));
    }

    /**
     * 初始化画笔
     *
     * @param lineWidth 画笔线宽
     * @param color 画笔颜色
     */
    private void initPaint(float lineWidth, Color color) {
        mStrokePaint = new Paint();
        mStrokePaint.setStyle(Paint.Style.STROKE_STYLE);
        mStrokePaint.setStrokeWidth(lineWidth);
        mStrokePaint.setColor(color);
        mStrokePaint.setDither(true);
        mStrokePaint.setFilterBitmap(true);
        mStrokePaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mStrokePaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        // 外圆
        mArc.setArc(mStartRotateAngle % 360, mRotateAngle % 360, false);
        canvas.drawArc(mOuterCircleRectF, mArc, mStrokePaint);
        canvas.restore();
    }

    @Override
    protected void setAlpha(int alpha) {
        mStrokePaint.setAlpha(alpha);
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {

    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        mStartRotateAngle = (int) (360 * animatedValue);
        switch (mCurrAnimatorState) {
            case 0:
                mRotateAngle = (int) (OUTER_CIRCLE_ANGLE * animatedValue);
                break;
            case 1:
                mRotateAngle = OUTER_CIRCLE_ANGLE - (int) (OUTER_CIRCLE_ANGLE * animatedValue);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRepeat(Animator animator) {
        if (++mCurrAnimatorState > FINAL_STATE) {

            // 还原到第一阶段
            mCurrAnimatorState = 0;
        }
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mStrokePaint.setColorFilter(colorFilter);
    }

}
